import Vue from 'vue'
import Vuex from 'vuex'
Vue.use(Vuex)

export default {
	namespaced: true,
	state: {
		webSetting: {
			brothertripintervaltime: 3
		},
		online: false,
		brotherTrip:{
			address:{
				poiName: '定位中...'
			}
		},
		goodsDetails: [],
		chooseOrder: [], //创建入库单时，选择需要入库的回收单
	},
	mutations: {
		// 小哥定位信息
		setBrotherTrip(state, value){
			state.brotherTrip = value
		},
		// 上线状态标识，ture为在线，false为离线
		setOnline(state, status){ 
			state.online = status
			uni.setStorageSync('brotherOnlineStatus',status)
		},
		// 储存临时选择回收物品列表数据
		setGoodsDetails(state, status){
			state.goodsDetails = status
		},
		//创建入库单时，选择的回收单数组
		setChooseOrder(state, status){
			state.chooseOrder = status
		}
	},
	actions:{
		// 获取小哥上线状态
		getBrotherOnlineState({
			commit,
			state
		})	{
			// Vue.prototype.$u.get('/Brother/GetData').then((res) => {
			// 	commit('setOnline', res.online)
			// })
			
			if(uni.getStorageSync('brotherOnlineStatus')){
				commit('setOnline', uni.getStorageSync('brotherOnlineStatus'))
			}
		},
		// 小哥行程记录
		getBrotherTrip({
			commit,
			state
		}) {
			return new Promise((resolve,reject) => {
				uni.getLocation({
					type: 'gcj02',
					geocode: true,
					success: res=> {
						console.log('小哥行程记录:')
						console.log('当前位置的经度：' + res.longitude);
						console.log('当前位置的纬度：' + res.latitude);
						
						let origin = {
							lng:res.longitude,
							lat:res.latitude
						}
						
						commit('setBrotherTrip',res)

						resolve(res)
						Vue.prototype.$u.post('/Brother/Trip/Online',origin).then((res)=>{
							console.log('TripCreate:',res)
							commit('setOnline', true)
						})
					},
					fail: res=>{
						console.log('getLocation-fail',res)
					},
					complete: res=>{
						resolve(false)
					}
				});
			})
		},
		// 今日统计
		getToDayCount({
			commit,
			state
		}) {
			return new Promise((resolve) => {
				Vue.prototype.$u.post('/Brother/BillDetails/ToDayCount').then((res) => {
					commit('setToDayCount', res.details)
					resolve(res.details)
				})
			})
		}
	}
}
